<?php
class form
{
	function editor($name = 'content', $textareaid = 'content', $value = '', $toolbar = 'standard', $width = '100%', $height = 400)
	{
        global $site_conf, $siteid;
        $siteid = (!isset($siteid) || empty($siteid)) ? $site_conf->get('SITE_ID') : $siteid;
        if(empty($textareaid))
        {
            $textareaid = $name;
        }
        $str = "<textarea name='$name' id='$textareaid'>$value</textarea>";
		$str .= "<script type=\"text/javascript\" src=\"".SITE_URL.EDITOR_PATH."fckeditor.js\"></script>\n<script language=\"JavaScript\" type=\"text/JavaScript\">var SiteUrl = \""
            .SITE_URL."\"; var sBasePath = \""
            .SITE_URL.EDITOR_PATH."\"; var oFCKeditor = new FCKeditor( '"
            .$textareaid."' ) ; oFCKeditor.BasePath = sBasePath ; oFCKeditor.Height = '"
            .$height."'; oFCKeditor.Width	= '"
            .$width."' ; oFCKeditor.ToolbarSet	= '"
            .$toolbar."' ;oFCKeditor.ReplaceTextarea();"
            ."var siteid='{$siteid}';";
		$str .= "</script>";
		return $str;
	}
    //$options $k=>$v  $value '1,2,3,4'
    function checkbox($options, $name, $id = '', $value = '', $cols = 5)
	{
		if(!$options) return '';
		if(!$id) $id = $name;
		$i = 1;
		if($value != '') $value = explode(',', $value);
        $data = '';
		foreach($options as $k=>$v)
		{
			$checked = ($value && in_array($k, $value)) ? 'checked' : '';
			$data .= "<label><input type=\"checkbox\"  name=\"{$name}[]\" value=\"{$k}\" {$checked}/> {$v}</label>\n ";
			if($i%$cols == 0) $data .= "<br />\n";
			$i++;
		}
		return $data;
	}

    function radio($options, $name, $id = '', $value = '', $cols = 5)
	{
		if(!$options) return '';
		if(!$id) $id = $name;
		$i = 1;
		if($value !== '') $value = explode(',', $value);
        $data = '';
		foreach($options as $k=>$v)
		{
			$checked = ($value && in_array($k, $value)) ? 'checked' : '';
			$data .= "<label><input type=\"radio\"  name=\"{$name}\" value=\"{$k}\" {$checked}/> {$v}</label>\n ";
			if($i%$cols == 0) $data .= "<br />\n";
			$i++;
		}
		return $data;
	}

    function check_yesno($name, $id = '', $label = '', $yes = 1, $no = 0, $value = 0)
    {
        if(!$id) $id = $name;
        $onclick = "$(\"#$id\").val($(this).attr(\"checked\") ? \"$yes\" : \"$no\")";
        $checked = $value ==  $yes ? 'checked' : '';
        $form = "<input type='checkbox' value='1' $checked onclick='$onclick'/> $label";
        $form .= "<input type='hidden' id='$id' name='$name' value='$value'/>";
        return $form;
    }

    function text($name, $id = '', $value = '', $size = 50, $class = '', $ext = '')
	{
		if(!$id) $id = $name;
		return "<input type=\"text\" name=\"$name\" id=\"$id\" value=\"$value\" size=\"$size\" class=\"$class\" $ext/> ";
	}

    function textarea($name, $id = '', $value = '', $wsize = 50, $hsize = 6, $class = '', $ext = '')
    {
        if(!$id) $id = $name;
        return "<textarea name=\"$name\" id=\"$id\" class=\"$class\" cols=\"$wsize\" rows=\"$hsize\" $ext>{$value}</textarea>";
    }
    
    function select($options, $name, $id = '', $value = '', $class = '',$key='')
	{
		if(!$id) $id = $name;
		$data = "<select name=\"$name\" id=\"$id\" size=1 $class>";
		foreach($options as $k=>$v)
		{
			$value_label = empty($key) ? $v : $v[$key];
			$selected = $k == $value ? 'selected' : '';
			$data .= "<option value=\"$k\" $selected>$value_label</option>\n";
		}
		$data .= '</select>';
		return $data;
	}
    
    /**
     * 根据type表的pcode生成无限层级的下拉列表
     * @author menghao
     * @global type $db
     * @param type $name
     * @param type $id
     * @param type $top type表的pcode字段
     * @param type $value
     * @return type 
     */
    function type_level_select($name, $id, $top = '', $value = '')
    {
        global $db;
        $list = array();
        $res = $db->query('select tcode,tname,parentcode from ' . DB_PRE .'type where topcode="'.$top.'" and status=' . STATUS_NORMAL . ' order by listorder desc, inputtime desc');
        if(is_array($res))
        {
            foreach ($res as $v)
            {
                    $list[$v['tcode']] = array($v['tname'],$v['parentcode']);
            }
        }
        return form::level_select($list, $name, $id, $top, $value);
    }
        
    function level_select($options,$name,$id,$top='',$value='')
    {
        $list = array();
        if(empty($value))
        {
            $data = '';
            foreach ($options as $k=>$v)
            {
                $list[] = array($k,$v[1],$v[0]);
                if($v[1] == $top)
                {
                    $data .= '<option value="'.$k.'">'.$v[0].'</option>';
                }
            }
            $data .= '</select>';
            $var = 'data_'.$name;
            $data = '<script>var '.$var.'= '.  json_encode($list).';</script><select name="'.$name.'[0]'.'" id="'.$id.'_1'.'" onchange="lselect(this,'.$var.')"><option value="">请选择</option>'.$data;
            return $data;
        }
        else
        {
            $array = array();
            form::get_child_select_level($options, $value, $top, $array);
            
            $i = 0;
            foreach ($options as $k=>$v)
            {
                $list[] = array($k,$v[1],$v[0]);
            }
            $var = 'data_'.$name;
            $data = '<script>var '.$var.'= '.  json_encode($list).';</script>';
            foreach($array as $v)
            {
                $id = $id . '_1';
                $data .= '&nbsp;<select name="'.$name.'['.$i.']'.'" id="'.$id.'" onchange="lselect(this,'.$var.')"><option value="">请选择</option>';
                foreach($v as $_v)
                {
                    $data .= $_v[3] == '1' ? '<option value="'.$_v[0].'" selected>'.$_v[1].'</option>' : '<option value="'.$_v[0].'">'.$_v[1].'</option>';
                }
                $data .= '</select>';
                $i++;
            }
            return $data;
        }
       
    }
    
    function get_child_select_level($data,$value,$top,&$array)
    {
        if(!isset($data[$value]))
        {
            return false;
        }
        $list = array();
        
        $current = $data[$value];
        foreach($data as $k=>$v)
        {
            if($v[1]==$current[1])
            {
                if($k == $value)
                {
                    $list[] = array($k,$v[0],$v[1],'1');
                }
                else
                {
                    $list[] = array($k,$v[0],$v[1],'0');
                }
            }
        }
        if($current[1] != $top)
        {
            form::get_child_select_level($data, $current[1], $top, $array);
        }
        $array[] =  $list;
    }

    function multi_select_group($options, $name, $id = '', $value = array(), $default = '', $style = '')
    {
        if(!$id) $id = $name;
        $form = "<select multiple='true' name='$name' id='$id' style='$style'>";
        if(!empty($default))
        {
            $form .= "<option value=''>$default</option>";
        }
        foreach($options as $gid => $group)
        {
            $form .= "<optgroup label='{$group['groupname']}]'>";
            if(!isset($group['groupchild']) || !is_array($group['groupchild']))
            {
                continue;
            }
            foreach($group['groupchild'] as $child)
            {
                if(!is_array($child)) 
                {
                    continue;
                }
                $selected = in_array($child['id'], $value) ? 'selected' : '';
                $form .= "<option value='{$child['id']}' $selected>{$child['name']}</option>";
            }
            $form .= "</optgroup>";
        }
        $form .= "</select>";
        return $form;
    }

    function select_template($name, $id = '', $value = '', $pre = '',$key='')
	{
        global $base_dir;
        $siteid = get_current_site();
        $site_root = site_root($siteid, 1);
		if(!$id) $id = $name;
		$templatedir = $site_root.'template/default/';
		$files = array_map('basename', glob($templatedir.$pre.'*.html'));
		$templates = array(''=>'请选择');
		foreach($files as $file)
		{
			$key = substr($file, 0, -5);
			$templates[$key] = $file;
		}
		ksort($templates);
		return form::select($templates, $name, $id, $value);
	}

    function image($name, $id, $value='' ,$width=450, $height=350)
    {
        $siteid = get_current_site();
        $form = form::text($name, $id, $value, 50);
        $site_url = SITE_URL;
        $form .= "<input type='button' onclick=\"javascript:openwinx('{$site_url}attachment.php?action=upload_thumb&fieldid=$id&site_id={$siteid}&maxwidth=$width&maxheight=$height','upload','450','350')\" value='上传图片'>";
        return $form;
    }

    function file($name, $id, $value='')
    {
        $siteid = get_current_site();
        $form = form::text($name, $id, $value, 50);
        $site_url = SITE_URL;
        $form .= "<input type='button' onclick=\"javascript:openwinx('{$site_url}attachment.php?action=upload_file&fieldid=$id&site_id={$siteid}','upload','450','350')\" value='上传文件'>";
        return $form;
    }

    function select_category( $name, $id, $default='', $value='')
    {
        include_once APP_ROOT . 'include/tree.class.php';
        global $db;
        if( empty( $id ) )
        {
            $id = $name;
        }
        $db->prepare( "select catid,catname,parentid from edufe_category where status = ?" );
        $db->set_int( STATUS_NORMAL );
        $data = $db->execute();
        $array = array();
        $default_option = '';
        if(!empty($default))
        {
            $default_option = "<option value=''>$default</option>";
        }
        if( is_array($data ) )
        {
            foreach( $data as $r )
            {
                $array[$r['catid']] = $r;
            }
        }
        $tree = new tree( $array );
        return "<select name='$name' id='$id' multiple='multiple' size='10' >" . $default_option . $tree->get_tree('', "<option value='\$catid' \$selected>\$spacer\$catname</option>", $value) . '</select>';
    }
    
    function select_single_type($pcode, $name, $id='', $default = '', $value='', $others='')
    {
    	global $db;
    	 $db->prepare('select tcode,tname from ' . DB_PRE . 'type where parentcode = ? and status=? order by listorder desc,inputtime desc');
        $db->set_string($pcode);
        $db->set_int(STATUS_NORMAL);
        $res = $db->execute();
        if(!$id) $id = $name;
		$data = "<select name=\"$name\" id=\"$id\" size=1 $others >";
		if(!empty($default))
		{
			$data .= "<option value=\"\">" . $default . "</option>";
		}
		foreach($res as $k=>$v)
		{
			$selected = $v['tcode'] == $value ? 'selected' : '';
			$data .= "<option value=\"{$v['tcode']}\" $selected>{$v['tname']}</option>\n";
		}
		$data .= '</select>';
		return $data;
    }
    
    function select_parent_types( $name, $id, $default='', $value='')
    {
        include_once APP_ROOT . 'include/tree.class.php';
        global $db;
        if( empty( $id ) )
        {
            $id = $name;
        }
        $db->prepare('select parentcode from ' . DB_PRE . 'type where parentcode != ? and status=? group by parentcode');
        $db->set_string('');
        $db->set_int(STATUS_NORMAL);
        $res = $db->execute();
        $pcodelist = array();
        if(is_array($res))
        {
        	foreach ($res as $r)
        	{
        		if(!empty($r['parentcode']))
        		{
        			$pcodelist[] = $r['parentcode'];
        		}
        	}
        }
        
        
        $db->prepare( "select *,parentcode as parentid from " . DB_PRE . "type where tcode in ('" . implode("','", $pcodelist) . "') and status = ?" );
        $db->set_int( STATUS_NORMAL );
        $data = $db->execute();
        $array = array();
        $default_option = '';
        if(!empty($default))
        {
            $default_option = "<option value=''>$default</option>";
        }
        if( is_array($data ) )
        {
            foreach( $data as $r )
            {
                $array[$r['tcode']] = $r;
            }
        }
        $tree = new tree( $array );
        return "<select name='$name' id='$id' >" . $default_option . $tree->get_tree('', "<option value='\$tcode' \$selected>\$spacer\$tname</option>", $value) . '</select>';
    }

    function select_drag_template($name, $id = '', $value = '', $pre = '')
	{
        global $base_dir;
		if(!$id) $id = $name;
		$templatedir = $_SERVER['DOCUMENT_ROOT'].$base_dir.'template/drag/';
		$files = array_map('basename', glob($templatedir.$pre.'*.html'));
		$templates = array(''=>'请选择');
		foreach($files as $file)
		{
			$key = substr($file, 0, -5);
			$templates[$key] = $file;
		}
		ksort($templates);
		return form::select($templates, $name, $id, $value);
	}
}
?>
